<?php
namespace backend\components;

use Yii;
use yii\base\Component;

/**
 * Class PayToUser
 * @package backend\components
 * 小程序微信提现
 */
class PayToUser extends Component
{
    public function unicode()
    {
        $str = uniqid(mt_rand(), 1);
        $str = sha1($str);
        return md5($str);
    }

    public function arrayToXml($data)
    {
        $str = '<xml>';
        foreach ($data as $k => $v) {
            $str .= '<' . $k . '>' . $v . '</' . $k . '>';
        }
        $str .= '</xml>';
        return $str;
    }

    public function xmlToArray($xml)
    {
        //禁止引用外部xml实体
        libxml_disable_entity_loader(true);
        $xmlString = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
        $val = json_decode(json_encode($xmlString), true);
        return $val;
    }

    public function curl($param = "", $url)
    {
        $postUrl = $url;
        $curlPost = $param;
        $ch = curl_init();                   //初始化curl
        curl_setopt($ch, CURLOPT_URL, $postUrl);         //抓取指定网页
        curl_setopt($ch, CURLOPT_HEADER, 0);          //设置header
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);      //要求结果为字符串且输出到屏幕上
        curl_setopt($ch, CURLOPT_POST, 1);           //post提交方式
        curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);      // 增加 HTTP Header（头）里的字段
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);    // 终止从服务端进行验证
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_SSLCERT, getcwd() . '/cert/apiclient_cert.pem'); //这个是证书的位置
        curl_setopt($ch, CURLOPT_SSLKEY, getcwd() . '/cert/apiclient_key.pem'); //这个也是证书的位置
        $data = curl_exec($ch);                 //运行curl
        curl_close($ch);
        return $data;
    }

    public function send($openid, $amount)
    {
        //http://localhost/duocai_bakc_new/api.php?c=wx_api_pay&f=sendPacket
        $time = time();
        $partnerTradeNo = $time . $time .$time.mt_rand(10, 99);
        $data['mch_appid'] = Yii::$app->params['mini']['wxAppId'];//'wx499********';//商户的应用appid
        $data['mchid'] = Yii::$app->params['mini']['mchId'];//'123******2';//商户ID
        $data['nonce_str'] = $this->unicode();//unicode();//这个据说是唯一的字符串下面有方法
        $data['partner_trade_no'] = $partnerTradeNo;//.time();//这个是订单号。
        $data['openid'] = $openid;//'ot5pZ5GIwUxqYXFRDu-F01ksI0Dg';//'osmxp1QUneYkB7OOiNiHtA*****';//这个是授权用户的openid。。这个必须得是用户授权才能用
        $data['check_name'] = 'NO_CHECK';//这个是设置是否检测用户真实姓名的
        //$data['re_user_name']='';//用户的真实名字
        $data['amount'] = $amount * 100;//提现金额 单位为分
        $data['desc'] = '奖励提现';//订单描述
        $data['spbill_create_ip'] = '120.79.18.133';//本地开发'113.119.156.184';//$_SERVER['SERVER_ADDR'];//这个最烦了，，还得获取服务器的ip
        $secretKey = Yii::$app->params['mini']['md5Key'];//'630c84211dd832b6a01*********';///这个就是个API密码。32位的。。随便MD5一下就可以了
        $data = array_filter($data);
        $str = '';
        ksort($data);
        foreach ($data as $k => $v) {
            $str .= $k . '=' . $v . '&';
        }
        $string = $str .'key=' . $secretKey;
        $data['sign'] = strtoupper(md5($string));
//        var_dump($string);
//        var_dump($data);
//        return;
        $xml = $this->arrayToXml($data);
        $url = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers';
        $res = $this->curl($xml, $url);
        $return = $this->xmlToArray($res);
//        echo '<pre>';
//        var_dump($return);
//        die;
        return $return;
    }
}